import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { basename, join } from 'path';
import { commands, Uri, window, workspace } from 'vscode';
import { config, MessageFormat } from 'vscode-nls';
import generate from '../../util/generate';


const localize = config({
	messageFormat: MessageFormat.file
})(__filename);

export default function add() {
	return commands.registerCommand('feidao.wxapp.page.add', async (uri: Uri) => {
		const folder = join(workspace.getWorkspaceFolder(uri).uri.fsPath, 'src-wx-app');

		const name = await window.showInputBox({
			placeHolder: localize(0),
			validateInput(val) {
				const p_path = join(folder, val);
				if (existsSync(p_path)) {
					return localize(2);
				} else {
					return null;
				}
			}
		});
		if (name) {
			if (!existsSync(folder)) {
				mkdirSync(folder);
			}
			// 创建页面逻辑层目录
			const p_path = join(folder, name);
			mkdirSync(p_path);
			create_a(p_path);		// 创建事件
			create_s(p_path);		// 创建响应
			create_p(p_path);	// 创建page

			// 创建页面原生目录
			const wx_path = join(join(folder, 'pages'), name);
			mkdirSync(wx_path);
			create_js(wx_path, name);	// 创建page.js
			create_json(wx_path, name);	// 创建page.json
			create_wxml(wx_path, name);	// 创建page.wxml
			create_wxss(wx_path, name);	// 创建page.wxss

			// 修改app.json和app-debug.json
			updata_app_json(folder, name);
			updata_app_debug_json(folder, name);

			window.showInformationMessage(localize(1));
			window.showTextDocument(Uri.file(join(p_path, 'p.ts')));

			workspace.saveAll();
		}
	});
}
function create_a(_path: string) {
	const path = generate(_path, 'a', '\\.ts', 3);
	const a = basename(path);
	const tpl = `import { IFeidaoAiWeappPage } from '@feidao/web/interfaces';

export default function ${a}(fd: IFeidaoAiWeappPage, ...args: any[]) {
	// todo
}
`;
	writeFileSync(path + '.ts', tpl);
}

function create_s(_path: string) {
	const tpl = `export default {
	'fd-events-init': 'a001'
};
`;
	writeFileSync(join(_path, 's.ts'), tpl);
}

function create_p(_path: string) {
	const tpl = `import init from '@feidao/web/framework/wxapp/d/page';

import s from './s';

/// FEIDAO IMPACTIONS BEGIN

import a001 from './a001';

/// FEIDAO IMPACTIONS END

(() => {
	/// FEIDAO ACTIONS BEGIN

	const actions = { a001 };

	/// FEIDAO ACTIONS END

	init(s, actions);
})();
`;
	writeFileSync(join(_path, 'p.ts'), tpl);
}

function create_js(_path: string, name: string) {
	const tpl = `require('../../js/feidao-app');
require('../../js/${name}');
`;
	writeFileSync(join(_path, `${name}.js`), tpl);
}

function create_json(_path: string, name: string) {
	const tpl = '{}';
	writeFileSync(join(_path, `${name}.json`), tpl);
}

function create_wxml(_path: string, name: string) {
	const tpl = `<!--pages/${name}.wxml-->
<text>pages/${name}.wxml</text>
`;
	writeFileSync(join(_path, `${name}.wxml`), tpl);
}

function create_wxss(_path: string, name: string) {
	const tpl = `/* pages/${name}.wxss */`;
	writeFileSync(join(_path, `${name}.wxss`), tpl);
}

function updata_app_json(_folder: string, name: string) {
	const app_json_path = join(_folder, 'app.json');
	const txt = readFileSync(app_json_path, {
		encoding: 'utf-8'
	});
	const obj = JSON.parse(txt) as {
		pages: string[]
	};
	obj.pages.push(`pages/${name}/${name}`);
	const str = JSON.stringify(obj, null, '\t');
	writeFileSync(app_json_path, str);
}

function updata_app_debug_json(_folder: string, _name: string) {
	// const app__debug_json_path = join(_folder, 'app-debug.json');
}
